package com.sticktogether.services;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;

import com.sticktogether.server.ServerCommunication;

public class StickTogether extends Service {
	
	LocationManager mlocManager;
	LocationListener mlocListener;
	
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
	
	@Override
	public void onCreate() {

		mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
		mlocListener = new LocationListener() {
			
			public void onLocationChanged(Location location) {
				ServerCommunication.updateGPS2(location.getLatitude(), location.getLongitude());
				stopSelf();
			}

			public void onProviderDisabled(String provider) {}

			public void onProviderEnabled(String provider) {}

			public void onStatusChanged(String provider, int status, Bundle extras) {}

		};
	}
	

	
	@Override
	public void onDestroy() {
		mlocManager.removeUpdates(mlocListener);
	}
	
	@Override
	/**
	 * Request updates from network coordinates.
	 * Store them on our server every time they change.
	 */
	public void onStart(Intent intent, int startId) {
		ServerCommunication.context = getApplicationContext();
		mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
	}

}
